﻿using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ZHDJ.Data.Models.Common;
using FineUI;
using ZHDJ.Core.Util;

namespace SysManage.Module.ScoreManage
{
    public partial class PartyObjectScoreRuleDescEdit : PageBase
    {
        #region ViewPower
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyObjectScoreRuleDescAdd";
            }
        }
        #endregion

        #region 页面属性
        public string EditorContent = string.Empty;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 绑定
        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            string id = GetQueryIntValue("id");
            if (!string.IsNullOrWhiteSpace(id))
            {
                //根据当前登录人获取数据
                PartyObjectScoreRuleDesc model = DB.PartyObjectScoreDescs.FirstOrDefault(i => i.ID == id);
                if (model != null)
                {
                    txtName.Text = model.Name;
                    txtNoName.Text = model.NoName;
                    txtOrder.Text = model.OrderBy.ToString();
                    EditorContent = model.Content;
                    ddlStatus.SelectedValue = model.Status.ToString();
                }
            }
        }
        #endregion

        #region 保存并关闭按钮
        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            string id = GetQueryIntValue("id");
            PartyObjectScoreRuleDesc model;

            if (!string.IsNullOrWhiteSpace(id))
            {
                //更新
                model = DB.PartyObjectScoreDescs.Find(id);
                if (model == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                model.Name = txtName.Text;
                model.NoName = txtNoName.Text;
                model.OrderBy = Convert.ToInt32(txtOrder.Text);
                model.Content = this.Request.Form["editorValue"];
            }
            else
            {
                //添加
                model = new PartyObjectScoreRuleDesc();
                model.ID = StringHelper.NewID;
                model.Name = txtName.Text;
                model.NoName = txtNoName.Text;
                model.OrderBy = Convert.ToInt32(txtOrder.Text);
                model.Content = this.Request.Form["editorValue"];
                model.CreateBy = CurrentUser.LoginID;
                model.CreateTime = DateTime.Now;
                DB.PartyObjectScoreDescs.Add(model);
            }
            model.Status = Convert.ToInt32(ddlStatus.SelectedValue);

            //if (DB.SaveChanges() > 0)
            //    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            if (DB.SaveChanges() > 0)
            {
                if (model.Status == 4)
                {
                    List<PartyObjectScoreRuleDesc> scores = DB.PartyObjectScoreDescs.Where(s => s.Status == 4 && s.ID != model.ID).ToList();
                    foreach (var score in scores)
                    {
                        score.Status = 1;
                    }
                    DB.SaveChanges();
                }

                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion

    }
}